package com.own.core.object.admin;

import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.own.core.html.form.dropDown.DropDown;
import com.own.core.html.form.object.ObjectField;
import com.own.core.object.Dao;
import com.own.core.object.admin.pages.Page;

@Entity
@Table(name = "core_role")
@NamedQueries({ @NamedQuery(name = "role.loadAuthPages", query = "select r from Role  r " +
																" join r.groups g " +
																" join g.users u " +
																" where u.id=:userId ") })
public class Role extends Dao {

	private long id;

	@DropDown(NamedQuery = "page.loadAllSubPage")
	private Page page;

	@DropDown(property = "displayName")
	private Action action;

	@ObjectField(iconSupport = true)
	private Set<Group> groups;

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	public long getId() {
		return id;
	}

	@Enumerated(EnumType.STRING)
	public Action getAction() {
		return action;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "group_role", joinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "group_id", referencedColumnName = "id") })
	public Set<Group> getGroups() {
		return groups;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "page_id", nullable = true)
	public Page getPage() {
		return page;
	}

	@Transient
	public String getName() {

		// return page.getName()+ "." + action.getName() ;
		return page.getName();
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public void setAction(Action action) {
		this.action = action;
	}

}
